package com.wujilin.okra.fragment;

import static android.app.Activity.RESULT_OK;
import static com.wujilin.okra.Okra.EXTRA_CATEGORY_ID;
import static com.wujilin.okra.Okra.EXTRA_CATEGORY_NAME;
import static com.wujilin.okra.Okra.EXTRA_OBJECTIVE_ID;
import static com.wujilin.okra.Okra.F_CATEGORY_ID;
import static com.wujilin.okra.Okra.LOADER_CATEGORY_CHOICE;
import static com.wujilin.okra.Okra.REQUEST_CATEGORY_CHOOSE;
import static com.wujilin.okra.Okra.TAG_DIALOG_CATEGORY_NEW;
import static com.wujilin.okra.Okra.T_OBJECTIVE;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.wujilin.okra.R;
import com.wujilin.okra.adapter.CategoryListAdapter;
import com.wujilin.okra.database.ModelCursor;
import com.wujilin.okra.database.Sql;
import com.wujilin.okra.loader.QueryCursorLoader;
import com.wujilin.okra.model.Model;

/**
 * 
 * @author Belin Wu
 */
public class CategoryChooseFragment extends DialogFragment implements LoaderCallbacks<Cursor>, 
		AdapterView.OnItemClickListener {
	private LinearLayout newCategoryLinear;
	private ListView categoryList;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getLoaderManager().initLoader(LOADER_CATEGORY_CHOICE, getArguments(), this);
	}
	
	public static CategoryChooseFragment newInstance(long objectiveId, long categoryId) {
		Bundle args = new Bundle();
		args.putLong(EXTRA_OBJECTIVE_ID, objectiveId);
		args.putLong(EXTRA_CATEGORY_ID, categoryId);
		
		CategoryChooseFragment fragment = new CategoryChooseFragment();
		fragment.setArguments(args);
		
		return fragment;
	}
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_category_choose, null);
		newCategoryLinear = (LinearLayout) view.findViewById(R.id.linear_new_category);
		categoryList = (ListView) view.findViewById(R.id.categories);
		
		newCategoryLinear.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				CategoryNewFragment categoryFragment = new CategoryNewFragment();
				categoryFragment.show(getFragmentManager(), TAG_DIALOG_CATEGORY_NEW);
				categoryFragment.setTargetFragment(CategoryChooseFragment.this, getTargetRequestCode());
			}
		});
		
		categoryList.setOnItemClickListener(this);
		
		Dialog dialog = new AlertDialog.Builder(getActivity())
				.setTitle(R.string.move_to)
				.setView(view)
				.create();
		return dialog;
	}

	@Override
	public Loader<Cursor> onCreateLoader(int id, Bundle args) {
		String sql = "select c._id, c.name, sum(case when o._id is null then 0 else 1 end) count from category c left join objective o on c._id=o.category_id group by c._id having c._id!=?";
		long categoryId = args.getLong(EXTRA_CATEGORY_ID);
   		return new QueryCursorLoader(getActivity(), sql, categoryId + "");
	}

	@Override
	public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
		ListAdapter adapter = new CategoryListAdapter(getActivity(), (ModelCursor) data);
		categoryList.setAdapter(adapter);
	}

	@Override
	public void onLoaderReset(Loader<Cursor> loader) {
		categoryList.setAdapter(null);
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
			case REQUEST_CATEGORY_CHOOSE:
				onCategoryChose(data);
				break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Model category = ((ModelCursor) parent.getItemAtPosition(position)).getModel();
		Intent data = new Intent();
		data.putExtra(EXTRA_CATEGORY_ID, id);
		data.putExtra(EXTRA_CATEGORY_NAME, category.getString("name"));
		onCategoryChose(data);
	}
	
	private void onCategoryChose(Intent data) {
		long categoryId = data.getLongExtra(EXTRA_CATEGORY_ID, 0);
		long objectiveId = getArguments().getLong(EXTRA_OBJECTIVE_ID);
		Sql.update(T_OBJECTIVE, objectiveId, F_CATEGORY_ID, categoryId);
		getTargetFragment().onActivityResult(getTargetRequestCode(), RESULT_OK, data);
		dismiss();
	}
}
